package cn.wolfcode.handler;

import cn.wolfcode.domain.OrderInfo;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

/**
 * @Author: Ruomeng
 * @Date: 2025/2/27
 * @Description : shopseckill
 * @Version: 1.0
 */
@Slf4j
@Component
@CanalTable(value = "t_order_info")
public class OrderInfoHandler implements EntryHandler<OrderInfo> {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void insert(OrderInfo orderInfo) {
        log.info("[订单数据同步] 收到订单新增数据：{}", JSON.toJSONString(orderInfo));
    }

    @Override
    public void update(OrderInfo before, OrderInfo after) {
        log.info("[订单数据同步] 收到修改订单数据 Before：{}", JSON.toJSONString(before));
        stringRedisTemplate.opsForValue().set("orders::detail:" + after.getOrderNo(), JSON.toJSONString(after));
        log.info("[订单数据同步] 收到修改订单数据 After：{}", JSON.toJSONString(after));
    }

    @Override
    public void delete(OrderInfo orderInfo) {
        log.info("[订单数据同步] 收到删除订单数据：{}", JSON.toJSONString(orderInfo));
        stringRedisTemplate.delete("orders::detail:" + orderInfo.getOrderNo());
    }

}
